home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-02-16 | 5.9 KB | 250 lines | [TEXT/PJMM] |
- {===============================================}
- {================= Bonus sprite unit ================}
- {===============================================}
-
- { Example file for Ingemars Sprite Animation Toolkit. }
- { © Ingemar Ragnemalm 1992 }
- { See doc files for legal terms for using this code. }
-
- unit sBonus;
-
- { Sprite unit. A sprite unit should include the following routines:}
- { Init-procedure, that initializes private bitmaps}
- { Setup-procedure, that sets variables other than the standard ones set by MakeSprite }
- { Handle-procedure, to be called once per iteration until the sprite dies }
- { Hittask-procedure (optional), for advanced collission handling. }
-
- interface
-
- uses
- {$IFC UNDEFINED THINK_PASCAL}
- Types, Quickdraw, Fonts, QuickDrawText, Dialogs, MixedMode, Resources,
- {$ELSEC}
- InterfacesUI,
- {$ENDC}
- SAT, MiscGraphics, sPoints, scores, SoundConst, GameGlobals;
-
- procedure InitBonus;
- procedure SetupBonus (me: SpritePtr);
- procedure HandleBonus (me: SpritePtr);
- procedure SetStrings;
-
- implementation
-
- var
- bonusFace: array[1..3] of FacePtr;
-
- procedure BuildSweetFace (s1, s2: Str255);
- var
- fnum: Integer;
- r, r2: Rect;
- info: FontInfo;
- s1w, s2w, maxWidth, theHeight: Integer;
- begin
- SetRect(r, 0, 0, 16, 16);
- if bonusFace[3] = nil then
- bonusFace[3] := SATNewFace(r);
- SATSetPortFace(bonusFace[3]); {I need a port to work in! The iconPort is ok.}
-
- GetFNum('Geneva', fnum);
- TextFont(fnum);
- TextSize(9);
- TextFace([]);
-
- s1w := StringWidth(s1) + 4;
- s2w := StringWidth(s2) + 4;
-
- if s1w > s2w then
- maxWidth := s1w
- else
- maxWidth := s2w;
- {if maxWidth > 64 then}
- {maxWidth := 64;}
- if maxWidth < 12 then
- maxWidth := 12;
-
- GetFontInfo(info);
- theHeight := (info.ascent + info.descent) * 2; {Fit 2 rows!}
- if theHeight < 10 then
- theHeight := 10;
-
- SATDisposeFace(bonusFace[3]);
-
- r.right := maxWidth;
- r.bottom := theHeight;
- bonusFace[3] := SATNewFace(r);
-
- SATSetPortFace(bonusFace[3]);
- {Font and size are already set - it is the same GrafPort, just new pixel data!}
-
- {Fill with gray}
- {Draw frame with lines}
- EraseRect(r);
- if gSAT.initDepth > 1 then
- begin
- {Medium-gray fill}
- RGBForeColor(MakeRGBColor($8000, $8000, $8000));
- PaintRect(r);
- {Dark-gray below}
- RGBForeColor(MakeRGBColor($4000, $4000, $4000));
- MoveTo(r.right - 1, r.top);
- LineTo(r.right - 1, r.bottom - 1);
- LineTo(r.left, r.bottom - 1);
- {Light-gray above}
- RGBForeColor(MakeRGBColor($C000, $C000, $C000));
- MoveTo(r.right - 1, r.top);
- LineTo(r.left, r.top);
- LineTo(r.left, r.bottom - 1);
-
- ForeColor(blackColor);
- end
- else
- FrameRect(r);
-
- {Draw text on two lines}
- r2 := r;
- r2.top := r2.bottom div 2;
- StringCenter(s2, r2, true, 0);
- r2.bottom := r2.top;
- r2.top := 0;
- StringCenter(s1, r2, true, 0);
-
- SATSetPortMask(bonusFace[3]);
- PaintRect(r);
- SATChangedFace(bonusFace[3]);
- SATSetPortScreen;
- end; {BuildSweetFace}
-
-
- procedure SetStrings;
- var
- d: DialogPtr;
- itemHandle: Handle;
- itemType, item: integer;
- itemRect: Rect;
- str: Str255;
- itemHit: Integer;
- {$IFC GENERATINGPOWERPC }
- filterProc: ProcPtr;
- {$ENDC}
- begin
- d := GetNewDialog(kSetStringsDialog, nil, WindowPtr(-1));
-
- GetDialogItem(d, 4, itemType, itemHandle, itemRect);
- SetDialogItemText(itemHandle, features^^.sweetString1);
- GetDialogItem(d, 5, itemType, itemHandle, itemRect);
- SetDialogItemText(itemHandle, features^^.sweetString2);
- SelectDialogItemText(d, 4, 0, 32767);
-
- {$IFC GENERATINGPOWERPC }
- filterProc := NewRoutineDescriptor(@Filter, uppModalFilterProcInfo, GetCurrentISA);
- {$ENDC}
-
- repeat
- {$IFC GENERATINGPOWERPC }
- ModalDialog(filterProc, itemHit);
- {$ELSEC}
- ModalDialog(@Filter, itemHit);
- {$ENDC}
- until itemHit in [ok, cancel];
-
- if itemHit = ok then
- begin
- GetDialogItem(d, 4, itemType, itemHandle, itemRect);
- GetDialogItemText(itemHandle, str);
- if length(str) > 15 then
- str := Copy(str, 1, 15);
- features^^.sweetString1 := str;
-
- GetDialogItem(d, 5, itemType, itemHandle, itemRect);
- GetDialogItemText(itemHandle, str);
- if length(str) > 15 then
- str := Copy(str, 1, 15);
- features^^.sweetString2 := str;
-
- ChangedResource(Handle(features));
- UpdateResFile(gPrefFile);
-
- BuildSweetFace(features^^.sweetString1, features^^.sweetString2);
- end;
-
- DisposeDialog(d);
- end; {SetStrings}
-
-
- procedure InitBonus;
- var
- ii: integer;
- h: handle;
- begin
- for ii := 1 to 2 do
- bonusFace[ii] := SATGetFace(127 + ii);
- {bonusFace[3] is built by the following call:}
- BuildSweetFace(features^^.sweetString1, features^^.sweetString2);
- end; {InitBonus}
-
- procedure SetupBonus (me: SpritePtr);
- var
- i: integer;
- begin
- i := SATRand(3) + 1;
- me^.face := bonusFace[i];
-
- if me^.position.h < 300 then
- me^.speed.h := 5
- else
- me^.speed.h := -5;
-
- {Set the vertical speed depending on the icon.}
- case i of
- 1:
- me^.speed.v := 0;
- 2:
- me^.speed.v := 10 * SATRand(2) - 5; {-5 or 5}
- 3:
- me^.speed.v := 6 * SATRand(2) - 3; {-3 or 3}
- end; {case}
-
- me^.hotRect := me^.face^.iconMask.bounds;
- {SetRect(me^.hotRect, -13 + 16, -28 + 32, 13 + 16, 0 + 32);}
-
- me^.task := @HandleBonus;
- end; {SetupBonus}
-
- procedure HandleBonus (me: SpritePtr);
- var
- mp: Spriteptr;
- begin
- if me^.kind <> -4 then
- begin
- me^.task := nil; { Caught by the player! }
- AddScore(50);
- mp := SATNewSprite(0, me^.position.h, me^.position.v, @SetupPoints);
- SATSoundPlay(jaSndH, 5, true);
-
- bonusesCollected := bonusesCollected + 1;
- end
- else
- begin
- bonusObjectRunning := true;
- if not features^^.macho then
- levelCompleted := false;
- end;
-
- me^.mode := me^.mode + 1;
-
- me^.position.h := me^.position.h + me^.speed.h;
- if me^.position.h > gSAT.offSizeH then
- me^.task := nil;
- if me^.position.h < -32 then
- me^.task := nil;
-
- me^.position.v := me^.position.v + me^.speed.v;
- if me^.position.v > gSAT.offSizeV - me^.face^.iconMask.bounds.bottom then
- me^.speed.v := -Abs(me^.speed.v);
- if me^.position.v < 0 then
- me^.speed.v := Abs(me^.speed.v);
-
- end; {HandleBonus}
-
- end.